<?php
/**
 * 自定义Exception类
 * 
 * @copyright (c) 2006 - 2007 与非网 (www.eefocus.com)
 * @author	kokko<kokko313@gmail.com>
 * @version 1.0 2007-11-7
 * @package com
 * @subpackage eefocus
 */
class Exceptions extends Exception{
	/**
	 * 异常类型
	 * 
	 * @var string
	 * @access private
	 */
	private $type;
	
	/**
	 * 是否存在多余调试信息
	 * 
	 * @var string
	 * @access private
	 */
	private $extra;
	
	function __construct( $message,$code=0,$extra=false ){
		parent::__construct($message,$code);
		$this->type = get_class($this);
		$this->extra = $extra;
	}
	
	/**
	 * 异常输出 所有异常处理类均通过toString方法输出错误
	 * 每次异常都会写入系统日志
	 * 该方法可以被子类重载
	 * 
	 * @access public 
	 * @return array
	 */
	function toString(){
		$error = '';
		$trace = $this->getTrace();
		if($this->extra) {
			// 通过throw_exception抛出的异常要去掉多余的调试信息
			array_shift($trace);
		}
		$this->class = $trace[0]['class'];
		$this->function = $trace[0]['function'];
		$this->file = $trace[0]['file'];
		$this->line = $trace[0]['line'];
		$file   =   file($this->file);
		$traceInfo='';
		$time = date("y-m-d H:i:m");
		foreach($trace as $t) {
			$traceInfo .= '['.$time.'] '.$t['file'].' ('.$t['line'].') ';
			$traceInfo .= $t['class'].$t['type'].$t['function'].'(';
			$traceInfo .= implode(', ', $t['args']);
			$traceInfo .=")\n";
		}
		$error['message']   = $this->message;
		$error['type']      = $this->type;
		$error['detail']    = lang('_MODULE_').'['.MODULE_NAME.'] '.lang('_ACTION_').'['.ACTION_NAME.']'."\n";
		$error['detail']   .=   ($this->line-2).': '.$file[$this->line-3];
		$error['detail']   .=   ($this->line-1).': '.$file[$this->line-2];        
		$error['detail']   .=   '<font color="#FF6600" >'.($this->line).': <b>'.$file[$this->line-1].'</b></font>';
		$error['detail']   .=   ($this->line+1).': '.$file[$this->line];
		$error['detail']   .=   ($this->line+2).': '.$file[$this->line+1];
		$error['class']     =   $this->class;
		$error['function']  =   $this->function;
		$error['file']      = $this->file;
		$error['line']      = $this->line;
		$error['trace']     = $traceInfo;

		//记录系统日志            
		$errorStr   = "\n".lang('_ERROR_INFO_')."[ Module:".MODULE_NAME.'  Action:'.ACTION_NAME.' ]'.$this->message."\n";
		$errorStr  .= lang('_ERROR_URL_').$_SERVER["PHP_SELF"]."\n";
		$errorStr  .= lang('_ERROR_TYPE_').$this->type."\n";
		$errorStr  .= lang('_ERROR_TRACE_')."\n".$traceInfo;
		logMessage($errorStr,5);
		return $error;
	}

	function __destruct(){
		
	}
}
?>